package com.ygxsk.carhome.greendao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.ygxsk.carhome.response.CarBrandLargeCategory;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-05-10  17:23
 * @Description:车辆品牌的管理器
 */
public class CarBrandLargeCategoryDaoManager {

    private static CarBrandLargeCategoryDaoManager sInstance;

    private DaoSession mDaoSession;

    public static CarBrandLargeCategoryDaoManager getInstance() {
        if (sInstance == null) {
            synchronized (CarBrandLargeCategoryDaoManager.class) {
                if (sInstance == null) {
                    sInstance = new CarBrandLargeCategoryDaoManager();
                }
            }
        }
        return sInstance;
    }

    public void init(Context context) {

        try {
            //do

            //这里不查看日志信息
            //QueryBuilder.LOG_SQL = PropertyApplication.getInstance().isDebug;
            //QueryBuilder.LOG_VALUES = PropertyApplication.getInstance().isDebug;
            //这个是MySQLiteOpenHelper的调试日志；
            //MigrationHelper.DEBUG = PropertyApplication.getInstance().isDebug;

            //这种是不带迁移的
            //DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(context, "workofflineinfo.db", null);
            //携带迁移
            MySQLiteOpenHelper devOpenHelper = new MySQLiteOpenHelper(context, "cardbrandinfo.db", null);
            SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
            DaoMaster daoMaster = new DaoMaster(writableDatabase);
            mDaoSession = daoMaster.newSession();

        } catch (Exception e) {
            //catch
            e.printStackTrace();
        }


    }


    //直接插入list数据
    public void insertMultCarBrand(List<CarBrandLargeCategory> _listInAdapter) {

        try {
            //do

            if (mDaoSession.getCarBrandLargeCategoryDao() != null) {

                mDaoSession.runInTx(new Runnable() {
                    @Override
                    public void run() {

                        if (_listInAdapter != null && !_listInAdapter.isEmpty()) {
                            for (CarBrandLargeCategory carBrandLargeCategory : _listInAdapter) {
                                mDaoSession.getCarBrandLargeCategoryDao().insertOrReplace(carBrandLargeCategory);
                            }
                        } else {
//                            LogUtils.d(ConstantSign.LOGCAT_NOMAL+"插入服务站的数据列表是空的-_listInAdapter");
                        }

                    }
                });

            }

        } catch (Exception e) {
            //catch
            e.printStackTrace();
        }

    }

    /**
     * 查询全部的数据内容
     */
    public ArrayList<CarBrandLargeCategory> getAllCardCarBrand() {

        try {

            //do
            if (mDaoSession != null && mDaoSession.getCarBrandLargeCategoryDao() != null) {

                //可以在界面上直接赋值因为正是我new出来的一个列表。
                ArrayList<CarBrandLargeCategory> carBrandLargeCategories = (ArrayList<CarBrandLargeCategory>) mDaoSession.getCarBrandLargeCategoryDao().loadAll();

                if (carBrandLargeCategories != null) {
                    return carBrandLargeCategories;
                } else {
                    return null;
                }

            }

        } catch (Exception e) {
            //catch
            e.printStackTrace();
        }

        return null;

    }

    /**
     * 删除一条数据信息
     */
    public void deleteCarBrand(CarBrandLargeCategory _carBrandLargeCategory) {

        try {
            //do
            if (mDaoSession != null && (mDaoSession.getCarBrandLargeCategoryDao() != null)) {
                mDaoSession.getCarBrandLargeCategoryDao().delete(_carBrandLargeCategory);
            }
        } catch (Exception e) {
            //catch
            e.printStackTrace();
        }

    }

    /**
     * 删除
     */
    public void deleteAllCarBrand() {

        try {

            if (mDaoSession != null && mDaoSession.getCarBrandLargeCategoryDao() != null) {
                //loginInfoDao.deleteAll(loginInfo.getClass());
                mDaoSession.getCarBrandLargeCategoryDao().deleteAll();
            }

        } catch (Exception e) {
            e.printStackTrace();
//            LogUtils.d("itchen", "删除本地所有数据失败：" + e.getMessage());
        }

    }

    /**
     * deleteAll()删除所有数据
     */
    public void deleteAllDbBean(CarBrandLargeCategory carBrandLargeCategory) {

        try {

            mDaoSession.deleteAll(carBrandLargeCategory.getClass());

        } catch (Exception e) {
//            LogUtils.d("LUO", "删除本地所有数据失败：" + e.getMessage());
            e.printStackTrace();
        }

    }

    /**
     * 模糊的查询列表
     */
    public List<CarBrandLargeCategory> queryListByKeyWord(String _keyWord) {

        try {

            //do
            //DaoSession daoSession = ((Application) getApplication()).getDaoSession();
            QueryBuilder<CarBrandLargeCategory> qb = mDaoSession.queryBuilder(CarBrandLargeCategory.class);
            QueryBuilder<CarBrandLargeCategory> studentQueryBuilder = qb.where(CarBrandLargeCategoryDao.Properties.CarBrandName.like("%" + _keyWord + "%"))//
                    .orderAsc(CarBrandLargeCategoryDao.Properties.CarBrandId);//

            List<CarBrandLargeCategory> bankRankList = studentQueryBuilder.list();

            return bankRankList;

        } catch (Exception e) {
            //catch
            e.printStackTrace();
        }

        return null;

    }

}
